/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'package:flutter/services.dart';

import '../hadss_intents.dart';

class Intents {
  final methodChannel = const MethodChannel('hadss_intents');

  // 共享已执行或预期的InsightIntent。
  // 为避免共享能力滥用，默认每个应用每天最多共享20次，超出限制后会返回错误码：1000101104。
  // 每次共享的数据条数不限，但是每次最多50KB数据量，单次数据量超出限制后会返回错误码：1000101105。
  // 所有接入方每天共享总次数上限为3000次，超出总次数限制会返回错误码：1000101106。
  // 建议积攒一定量的数据后一次全部共享并控制共享次数。
  Future<void> shareIntent(List<InsightIntent> shareList) {
    List<String> list = [];
    for (var item in shareList) {
      list.add(item.toString());
    }
    return methodChannel
        .invokeMethod<String>('shareIntent', {'shareList': list.toString()});
  }

  // 如果设置了identifiers参数，则删除intent名称下的identifiers对应的记录。
  // 否则，删除意图名称下的所有记录
  Future<void> deleteIntent(String intentName, {List<String>? identifiers}) {
    return methodChannel.invokeMethod<void>(
        'deleteIntent',
        identifiers != null
            ? {'intentName': intentName, 'identifiers': identifiers}
            : {'intentName': intentName});
  }

  // 按实体名称下的实体ID删除InsightIntent实体
  Future<void> deleteEntity(String entityName, List<String> entityIds) {
    return methodChannel.invokeMethod<void>(
        'deleteEntity', {'entityName': entityName, 'entityIds': entityIds});
  }

  // 获取Service Open ID
  Future<String?> getSide(bool renew) async {
    return methodChannel.invokeMethod<String>('getSide', {'renew': renew});
  }
}
